package com.dup.fastgpt.controller;

import com.dup.fastgpt.dto.FastGptRequest;
import com.dup.fastgpt.dto.FastGptResponse;
import com.dup.fastgpt.service.FastGptService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;


@RestController
@RequestMapping("/api/fastgpt")
@RequiredArgsConstructor
public class FastGptController {
    @Autowired
    private FastGptService fastGptService;

    /**
     * 会话请求
     * @param request
     * @return
     */
    @PostMapping("/send")
    public Mono<ResponseEntity<FastGptResponse>> sendMessage(@RequestBody FastGptRequest request) {
        return fastGptService.sendMessage(request)
                .map(ResponseEntity::ok)
                .onErrorResume(e -> Mono.just(ResponseEntity
                        .status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body(new FastGptResponse(null, null, null, null,null,null))));
    }

    /**
     * 包含历史上下文的会话请求
     * @param request
     * @return
     */
    @PostMapping("/chatSend")
    public Mono<ResponseEntity<FastGptResponse>> chatSend(@RequestBody FastGptRequest request) {
        return fastGptService.sendMessage(request)
                .map(ResponseEntity::ok)
                .onErrorResume(e -> Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body(new FastGptResponse(null, null, null, null,null,null))));
    }
}
